package org.jeecg.modules.web.Sms;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.web.shopApply.entity.ShopApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/22 1:39
 * @content
 */

@Api(tags="短信验证")
@RestController
@RequestMapping("schoolOrder/web/sms")
@Slf4j
public class SchoolOrderSmsController {


    static final String accessKeyId = "LTAI5tJEb7t5PDfTyRjt241n";
    static final String accessKeySecret = "4LkXnwTfMQU9eeyMzhl2ulBq3SJXvy";


    @Autowired
    private RedisUtil redisUtil;

    /**
     *   添加
     *
     * @return
     */
    @AutoLog(value = "验证码发送")
    @ApiOperation(value="验证码发送", notes="验证码发送")
    @GetMapping(value = "/sendSms")
    public Result<String> sendSms(@RequestParam(name="mobile") String mobile) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = this.createClient(accessKeyId,accessKeySecret);
        String code = getRandCode();
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers(mobile)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
     //   System.out.println(resp.body);
        redisUtil.set(mobile,code);
        return Result.OK("发送成功");
    }


    public static String getRandCode() {
        String code  = "";
        for(int i=0; i<6; i++){
            code += ((int)((Math.random() * 9)) + "");
        }
        return code;
    }

    public  com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
}
